package com.cskaoyan.service.promote;

import com.cskaoyan.bean.MallTopic;
import com.cskaoyan.mapper.MallTopicMapper;
import com.cskaoyan.model.bo.promote.SimpleMallTopic;
import com.cskaoyan.model.vo.promote_ad.BaseData;
import com.cskaoyan.model.vo.promote_ad.TopicRead;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 *推广管理/专题管理的业务逻辑层
 * @author：23117
 * @date: 2021/12/1
 */
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    MallTopicMapper mallTopicMapper;

    @Override
    public BaseData list(Integer page,Integer limit) {
        PageHelper.startPage(page,limit);

        List<MallTopic> mallTopics = mallTopicMapper.seletList();

        PageInfo info = new PageInfo(mallTopics);
        int total = (int) info.getTotal();
        BaseData data = new BaseData(total, mallTopics);

        return data;
    }

    @Override
    public boolean update(MallTopic mallTopic) {
        Integer i = mallTopicMapper.updateMallTopic(mallTopic);
        return i == 1;
    }

    @Override
    public TopicRead read(Integer id) {

        List<MallTopic> mallTopics = mallTopicMapper.readList(id);


        TopicRead topicRead = new TopicRead();
        topicRead.setTopic(mallTopics.get(0));
        return topicRead;
    }

    @Override
    public boolean delete(MallTopic mallTopic) {
        mallTopic.setDeleted(true);
        Integer i =mallTopicMapper.deleteMallTopic(mallTopic);
        return i ==1;
    }

    @Override
    public MallTopic create(SimpleMallTopic simpleMallTopic) {

        MallTopic mallTopic = new MallTopic();

        mallTopic.setContent(simpleMallTopic.getContent());

        mallTopic.setPicUrl(simpleMallTopic.getPicUrl());
        mallTopic.setPrice(simpleMallTopic.getPrice());
        mallTopic.setReadCount(simpleMallTopic.getReadCount());
        mallTopic.setSubtitle(simpleMallTopic.getSubtitle());
        mallTopic.setTitle(simpleMallTopic.getTitle());


        Integer i = mallTopicMapper.insertSimpleMallTopic(mallTopic);

        if (i==1) return mallTopic;
        return null;
    }
}


